var api = require('../../utils/api.js')
var app = getApp()
Page({
    data: {
        code:'',
        openid:'',
        name:'',
        passw:''
    },
    showtips(){
        wx.showToast({
          title: '忘记密码请联系管理员',
          icon:'none'
        })
    },
    onLoad() {
  
    },
    onShow(){
        //调用登录接口
        var that=this;
        wx.login({
            success: function (ret) {
            console.log(ret);
            wx.getUserInfo({
                success: function () {
                    // api.post({
                    //     url: 'wxapp/public/getopenid',
                    //     data: {'code':ret.code},
                    //     success: data => {
                    //         that.setData({
                    //             openid:data.data.openid
                    //         })
                    //     }
                    // })
                }
            })

            that.setData({
                code:ret.code
            })
            }
        })




    },

    getname(ret){
        this.setData({
            name:ret.detail.value
        })
    },

    getpassw(ret){
        this.setData({
            passw:ret.detail.value
        })
    },
    //用户登录
    login_btn(){
        var that=this;

        var passw=this.data.passw;
        var name=this.data.name;
        wx.showToast({
            title: '登录中',
            icon:'none'
          })

          console.log(passw,name) 

        api.post({
            url: 'user/login',
            data: {password:passw,account:name},
            success: data => {

                console.log(data) 
                var info=data.data;
                console.log(info) 

                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })
                if(data.code==1){
                    wx.setStorageSync('user', info.userinfo)
                    wx.setStorageSync('token', info.userinfo.token)
    
                    wx.switchTab({
                      url: '/pages/my/my',
                    })
                }
              

            }
        })
    },


    bindGetUserInfo: function(o) {
        var that=this;
        var openid=that.data.openid;
        var agentid=wx.getStorageSync('agentid');

        wx.getUserProfile({
            desc: '用于完善会员资料', 
            success: (res) => {
                var userInfo=JSON.parse(res.rawData);
    
                api.post({
                    url: 'wxapp/public/wxlogin',
                    data: {'openid':openid,'avatar':userInfo.avatarUrl,'nickName':userInfo.nickName,agentid:agentid},
                    success: data => {
                        var info=data.data;
                        console.log(info) 
                        wx.setStorageSync('user', info.user)
                        wx.setStorageSync('token', info.token)

                        wx.switchTab({
                          url: '/pages/my/my',
                        })

                    }
                })
            }
        })


    },


    goindex(){
        wx.switchTab({
          url: '/pages/index/index',
        })
    },
    getPhoneNumber: function(e){
        console.log(e.detail);
        var code=this.data.code;
        api.post({
            url: 'wxapp/getmobile',
            data: {'code':code},
            success: data => {
                console.log(data)
            }
        })
    },
})
